<template>
  <cement-content
    :title="wellborestructureTitle"
    :expandTitle="wellborestructureExpandTitle"
    :search="wellborestructureSearch"
    @getData="getWellBoreStructure"
    @deleteRow="deleteWellBoreStructure"
    @submitUpdate="submitWellBoreStructureUpdate"
    @submitSearchModel="submitWellBoreStructureSearchModel">
  </cement-content>
</template>

<script>
import cementContent from '@/components/cement-content'
import {WELL_BORE_STRUCTURE_TITLE, EXPAND_WELL_BORE_STRUCTURE_TITLE, BOREHOLE_SEARCH_DATA, SEPARATOR, FIRST_PAGE} from '@/util/constant'
import {mapGetters, mapMutations} from 'vuex'
import {getWellBoreStructure, deleteWellBoreStructure, updateWellBoreStructure} from '@/api/module/well-bore-structure'
import {get, cloneDeep} from 'lodash'

export default {
  name: 'index',
  components: {
    cementContent
  },
  data () {
    return {
      wellborestructureTitle: WELL_BORE_STRUCTURE_TITLE,
      wellborestructureExpandTitle:EXPAND_WELL_BORE_STRUCTURE_TITLE,
      wellborestructureSearch: BOREHOLE_SEARCH_DATA
    }
  },
  methods: {
    ...mapMutations([
      'changeTableData',
      'changeTotal',
      'closeDialog',
      'changePageSize'
    ]),
    getWellBoreStructure () {
      let search = cloneDeep(this.searchModelGetters)
      if (get(this.searchModelGetters, 'drillDate')) {
        search['drillDate'] = this.searchModelGetters['drillDate'][0] + SEPARATOR + this.searchModelGetters['drillDate'][1]
      }
      getWellBoreStructure(this.currentPageGetters - 1, this.pageSizeGetters, search).then((res) => {
        this.changeTableData(get(res, 'data.data.content', []))
        this.changeTotal(get(res, 'data.data.totalElements', 0))
      })
    },
    deleteWellBoreStructure (row) {
      if (get(row, 'WellBoreSturctureId')) {
        deleteWellBoreStructure(row['WellBoreSturctureId'])
      }
    },
    submitWellBoreStructureSearchModel () {
      this.changePageSize(FIRST_PAGE)
      this.getWellBoreStructure()
    },
    submitWellBoreStructureUpdate () {
      if (get(this.updateModelGetters, 'WellBoreSturctureId')) {
        updateWellBoreStructure(get(this.updateModelGetters, 'WellBoreSturctureId'), this.updateModelGetters).then(
          this.closeDialog()
        )
      }
    }
  },
  computed: {
    ...mapGetters([
      'pageSizeGetters',
      'currentPageGetters',
      'searchModelGetters',
      'updateModelGetters'
    ])
  },
  mounted () {
    this.getWellBoreStructure()
  }
}
</script>
